package com.document;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.TextView;
import android.widget.Toast;

public class ViewFileText extends Activity{
	private WebView view;
	private String filepath;
	private TextView t;
	@SuppressLint("NewApi")
	public void onCreate(Bundle savedInstanceState) {
		 final Activity activity = this;
		super.onCreate(savedInstanceState);		
		getWindow().requestFeature(Window.FEATURE_PROGRESS);  		
        // 完成窗体的全屏显示  取消掉状态栏  
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                WindowManager.LayoutParams.FLAG_FULLSCREEN); 
		setContentView(R.layout.view);		
		view = (WebView)findViewById(R.id.show);
		t=(TextView)findViewById(R.id.filename_1);
		view.setWebChromeClient(new WebChromeClient() {   
              
	            public void onProgressChanged(WebView view, int newProgress) {   
	                // 设置WebView加载进度条 
	                activity.setProgress(newProgress * 100);   
	            }   
	        });  
			
		 final String path = GetPath.getpath()+File.separator;
		 System.out.println(GetPath.getpath());
       
         Intent i=getIntent();
         Uri uri=(Uri)i.getData();
          filepath=uri.getPath(); 
         File f=new File(filepath);
         String fn=f.getName();
         t.setText(fn);
         //System.out.println(f);
         System.out.println(filepath);
         readtxt(filepath);
		 	//设置网页缩放
		view.getSettings().setSupportZoom(true);
		view.getSettings().setBuiltInZoomControls(true);
		view.getSettings().setDefaultTextEncodingName("UTF-8");
		view.getSettings().setUseWideViewPort(true);

	//view.getSettings().setLoadWithOverviewMode(true);
		view.loadUrl("file:/"+GetPath.getpath()+File.separator+"dhfj59rkek5orkt.html");
	}
	//重写onCreateContextMenu，实现复制文字
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		
		//实例化工具类
				WebViewCopy copy = new WebViewCopy(this, view);
			copy.onCreateContextMenu(menu, v, menuInfo,0,"复制"); 
			super.onCreateContextMenu(menu, v, menuInfo);
			}
	public void readtxt(String filepath){
		StringBuffer buffer = new StringBuffer();
	      try {
	      FileInputStream fis = new FileInputStream(filepath);
InputStreamReader isr = new InputStreamReader(fis,"GB2312");//文件编码Unicode,UTF-8,ASCII,GB2312,Big5
	      Reader in = new BufferedReader(isr);
	      int ch;
	      while ((ch = in.read()) > -1) {
	          buffer.append((char)ch);
	                            }
	      in.close();
	      String text=buffer.toString();
	      System.out.println("text"+text);
	     String html="<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=GBK\"></head><body>  "+text
	     +"</body></html>";
	     File _html = new File(GetPath.getpath()+File.separator+"dhfj59rkek5orkt.html");
			FileOutputStream stream;
			OutputStreamWriter writer;
			
			      stream = new FileOutputStream(_html); 
			      writer = new OutputStreamWriter(stream,"gbk"); 
			      writer.write(html);
			      writer.flush();
			      writer.close();
		
	      } catch (IOException e) {
	    
	      }
	}
	//添加菜单
		 public boolean onCreateOptionsMenu(Menu menu)    
		    {    
		            
		        menu.add(0, 0, 0, "截屏");  
		        menu.add(0, 1, 0, "发送");  
		        
		        	          
		        return super.onCreateOptionsMenu(menu);    
		    }    
		    
		 
		    @Override    
		    public boolean onOptionsItemSelected(MenuItem item)    
		    {    
		        switch(item.getItemId())    
		        {    
		            case 0:   
		                {   
		                	SimpleDateFormat formatter=new SimpleDateFormat("MM月dd日HH.mm.ss");       
		                	Date curDate=new Date(System.currentTimeMillis());//获取当前时间       
		                	String str=formatter.format(curDate);
		                	System.out.println(str);
		               	 ShotView.shot(this,"mnt/sdcard/DCIM/Txt截图"+str+".png");
		                  
	             Toast.makeText(getApplicationContext(), "截屏成功,已保存在mnt/sdcard/DCIM/Word截图"+str+".png", Toast.LENGTH_LONG).show();
	             
		                    break;    
		                }  
		            case 1:   
	                {   
	                	//调用系统分享功能
			    		 Intent intent = new Intent(Intent.ACTION_SEND);  
			                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
			                intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(filepath)));   
			                intent.setType("*/*");   //分享文件  
			                startActivity(Intent.createChooser(intent,"分享"));
			    		break;
	         
	                       
	                } 
		        }
				return false;}
	
		
		}

